
package me.zhengjie.service.impl;

import me.zhengjie.domain.AppMessageTo;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.AppMessageToRepository;
import me.zhengjie.service.AppMessageToService;
import me.zhengjie.service.dto.AppMessageToDto;
import me.zhengjie.service.dto.AppMessageToQueryCriteria;
import me.zhengjie.service.mapstruct.AppMessageToMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author IQW
* @date 2022-07-18
**/
@Service
@RequiredArgsConstructor
public class AppMessageToServiceImpl implements AppMessageToService {

    private final AppMessageToRepository appMessageToRepository;
    private final AppMessageToMapper appMessageToMapper;

    @Override
    public Map<String,Object> queryAll(AppMessageToQueryCriteria criteria, Pageable pageable){
        Page<AppMessageTo> page = appMessageToRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(appMessageToMapper::toDto));
    }

    @Override
    public List<AppMessageToDto> queryAll(AppMessageToQueryCriteria criteria){
        return appMessageToMapper.toDto(appMessageToRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AppMessageToDto findById(Integer id) {
        AppMessageTo appMessageTo = appMessageToRepository.findById(id).orElseGet(AppMessageTo::new);
        ValidationUtil.isNull(appMessageTo.getId(),"AppMessageTo","id",id);
        return appMessageToMapper.toDto(appMessageTo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppMessageToDto create(AppMessageTo resources) {
        return appMessageToMapper.toDto(appMessageToRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AppMessageTo resources) {
        AppMessageTo appMessageTo = appMessageToRepository.findById(resources.getId()).orElseGet(AppMessageTo::new);
        ValidationUtil.isNull( appMessageTo.getId(),"AppMessageTo","id",resources.getId());
        appMessageTo.copy(resources);
        appMessageToRepository.save(appMessageTo);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            appMessageToRepository.deleteById(id);
        }
    }

    @Override
    public void read(Integer msgId) {

        Integer userId = SecurityUtils.getCurrentUserId().intValue();
        AppMessageTo msgCheck = appMessageToRepository.queryByUserIdAndMsgId(userId,msgId);
        if(null == msgCheck){
            throw new BadRequestException("消息不存在");
        }
//        AppMessageTo msg = new AppMessageTo();
//        msg.setUserId(userId);
//        msg.setMsgId(msgId);
//        msg.setNickName(SecurityUtils.getCurrentUsername());
        msgCheck.setStatus(1);
        msgCheck.setReadTime(DateUtils.getNowTimestamp());
        msgCheck.setCreateTime(DateUtils.getNowTimestamp());
        appMessageToRepository.save(msgCheck);
    }

    @Override
    public Integer msgCount(Integer userId) {
        return  appMessageToRepository.msgCount(userId);
    }

    @Override
    public void download(List<AppMessageToDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AppMessageToDto appMessageTo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", appMessageTo.getUserId());
            map.put("用户昵称", appMessageTo.getNickName());
            map.put("消息ID", appMessageTo.getMsgId());
            map.put("读取时间", appMessageTo.getReadTime());
            map.put("状态", appMessageTo.getStatus());
            map.put(" createTime",  appMessageTo.getCreateTime());
            map.put(" updateTime",  appMessageTo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}